﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Maknys.Consultator.Services;
using Maknys.Consultator.ServiceContracts;
using Maknys.Consultator.DomainModel;
using System.Collections;


namespace mainWindow
{
    public partial class DataBase : Form
    {
        private  IEditingService editingService;
        private ArrayList infoList = new ArrayList(); // List with DB information

        public DataBase()
        {
            InitializeComponent();
        }

        private void DataBase_Load(object sender, EventArgs e)
        {
            editingService = new EditingService();
            main();
        }

        public void main()
        {
            editingService.ReadAll(infoList);
            print(infoList);

        }

        public void print(ArrayList infoList)
        {
            foreach (Element inf in infoList)
            {

                DBList.Items.Add(inf.Id + "  " + inf.PId + "  " + inf.Value);
                
            }
        }

        private void DBList_SelectedIndexChanged(object sender, EventArgs e)
        {
            string id = "";
            string pId = "";
            string value = "";
            string sentence = (string)DBList.SelectedItem;
            editingService.MakeSentence(sentence, ref id, ref pId, ref value);
            IDTextBox.Text = id;
            PIDTextBox.Text = pId;
            ValueTextBox.Text = value;
        }

        private void New_Click(object sender, EventArgs e)
        {
            string pId = PIDTextBox.Text;
            string value = ValueTextBox.Text;
            editingService.MakeNewElement(pId, value);
            infoList.Clear();
            editingService.ReadAll(infoList);
            DBList.Items.Clear();
            print(infoList);
        }

        private void Edit_Click(object sender, EventArgs e)
        {
            string id = IDTextBox.Text;
            string pId = PIDTextBox.Text;
            string value = ValueTextBox.Text;

            editingService.EditElement(id, pId, value);
            infoList.Clear();
            editingService.ReadAll(infoList);
            DBList.Items.Clear();
            print(infoList);
        }

        private void Delete_Click(object sender, EventArgs e)
        {
            string id = IDTextBox.Text;
            editingService.DeleteElement(id);
            infoList.Clear();
            editingService.ReadAll(infoList);
            DBList.Items.Clear();
            print(infoList);
        }
    }
}
